package com.hh.item.controller;

import com.alibaba.fastjson.JSON;
import com.hh.item.pojo.User;
import com.hh.item.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author yangfan
 * @date 2020/3/7 15:23
 * @description
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * @param userName
     * @param userPwd
     * @return com.hh.item.pojo.User
     * @description 用户登陆校验
     */
    @GetMapping("/login/{userName}/{userPwd}")
    public User login(@PathVariable String userName, @PathVariable String userPwd) {
        return userService.login(userName, userPwd);
    }

    /**
     * @param userName
     * @return java.lang.String
     * @description 注册前检验用户名是否存在
     */
    @GetMapping("/check/{userName}")
    public String check(@PathVariable String userName) {
        return userService.check(userName);
    }

    /**
     * @param user
     * @return java.lang.String
     * @description 用户注册
     */
    @PostMapping("/regist")
    public String regist(@RequestBody User user) {
        return userService.regist(user);
    }

    /**
     * @param userId
     * @param searchText
     * @return java.lang.String
     * @description 按照用户名模糊查找用户信息 并返回与查询者的关系
     */
    @GetMapping("/search/{userId}/{searchText}")
    public String search(@PathVariable String userId, @PathVariable String searchText) {
        return userService.searchUser(userId, searchText);
    }

    /**
     * @param userId
     * @param targetId
     * @return java.lang.String
     * @description 关注
     */
    @GetMapping("/follow/{userId}/{targetId}")
    public String follow(@PathVariable String userId, @PathVariable String targetId) {
        return userService.follow(userId, targetId);
    }

    /**
     * 取关
     * @param userId
     * @param targetId
     * @return
     */
    @GetMapping("/unfollow/{userId}/{targetId}")
    public String unfollow(@PathVariable String userId, @PathVariable String targetId) {
        return userService.unfollow(userId, targetId);
    }

    /**
     * 修改个人信息
     * @param oldName
     * @param userName
     * @param userPwd
     * @param userSex
     * @return
     */
    @GetMapping("/edit/{oldName}/{userName}/{userPwd}/{userSex}")
    public User edit(@PathVariable String oldName, @PathVariable String userName,@PathVariable String userPwd, @PathVariable Integer userSex){
        return userService.edit(oldName,userName,userPwd,userSex);
    }

    /**
     * 获取我的信息
     * @param userId
     * @return
     */
    @GetMapping("/getMine/{userId}")
    public User getMine(@PathVariable Integer userId){
        return userService.getMine(userId);
    }

    /**
     * 获取关注列表
     * @param userId
     * @return
     */
    @GetMapping("/getFollow/{userId}")
    public List<User> getFollow(@PathVariable Integer userId){
        return userService.getFollow(userId);
    }

    /**
     * 获取粉丝列表
     * @param userId
     * @return
     */
    @GetMapping("/getFans/{userId}")
    public List<User> getFans(@PathVariable Integer userId){
        return userService.getFans(userId);
    }

    /**
     * 将所有用户id与用户名缓存到redis
     * @return
     */
    @GetMapping("/searchAll")
    public List<User> searchAllNameAndId(){
        return userService.searchAllNameAndId();
    }







    /*@GetMapping("/test")
    @ResponseBody
    public String test() {
        return JSON.toJSONString(userService.testTk());
    }

    @RequestMapping("/findAll")
    public ModelAndView findAll() {
        ModelAndView mv = new ModelAndView();
        List<User> userList = userService.findAll();
        mv.addObject("userList", userList);
        mv.setViewName("showUser");
        return mv;
    }

    *//*@RequestMapping("/regist")
    public void regist(User user, HttpServletRequest request, HttpServletResponse response) throws Exception {
        user.setMsgCount(0);
        user.setFollowCount(0);
        user.setFansCount(0);
        int result = userService.save(user);
        if (result == 1) {
            response.sendRedirect(request.getContextPath() + "/login.jsp");
        } else {
            request.setAttribute("errorMsg", "注册失败");
            request.getRequestDispatcher("/regist.jsp").forward(request, response);
        }
    }*//*


    @RequestMapping("/refresh/{userId}")
    public void refresh(@PathVariable String userId, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        Map<String, String> ffMap = userService.refresh(userId);
        String ffMapJson = JSON.toJSONString(ffMap);
        response.getWriter().println(ffMapJson);
    }

    @RequestMapping("/refreshf/{userId}/{targetId}")
    public void refreshf(@PathVariable String userId, @PathVariable String targetId, HttpServletResponse response) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        Map<String, Object> fMap = userService.refreshf(userId, targetId);
        String fMapJson = JSON.toJSONString(fMap);
        response.getWriter().println(fMapJson);
    }*/
}
